package com.zy.cat.common.utils.sms;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;
import com.taobao.api.internal.util.WebUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * 任毅短信平台对接
 * 
 * @author Link
 * @date 2019-06-24 09:58
 */

@Slf4j
public class RYSMSUtils {

	private String username = "jnyq1";
	private String pwd = "dtfrqm";
	final private static String url = "http://api.sms.mas521.com:9000/smsSend.do";

	public static void main(String[] args) {
		RYSMSUtils.getInstance(1).sendWarningMS("18954109496", "紧急通知", "测试");
// 		RYSMSUtils.getInstance(1).sendCodeMS("13854107520", "123456", "微信发送");
	}
	
	/**
	 * 
	 * >0	成功,系统生成的任务id或自定义的任务id。
		如果指定的msgid为字符串，则返回系统内部的任务id
		0	失败
		-1	用户名或者密码不正确
		-2	必填选项为空
		-3	短信内容0个字节
		-5	余额不够
		-10	用户被禁用
		-11	短信内容超过500字
		-12	无扩展权限（ext字段需填空）
		-13	IP校验错误
		-14	内容解析异常
		-990	未知错误
	 */

	private RYSMSUtils(String username, String pwd) {
		this.username = username;
		this.pwd = pwd;
	}

	/**
	 * 
	 * @info
	 * @author Link
	 * @date 2019-09-05 15:43
	 * @param type 0.APP 1.公众号
	 * @return
	 */
	public static RYSMSUtils getInstance(int type) {
		String username = "jnyqhy";
		String pwd = "z2q9vx";
		if (type == 1) {
			username = "jnyq1";
			pwd = "dtfrqm";
		}
		return new RYSMSUtils(username, pwd);
	}

	/**
	 * 
	 * @author Link
	 * @date 2019-06-24 11:43
	 * @param phone 手机号码
	 * @param code  验证码
	 * @param name  签名
	 * @return
	 */
	public synchronized boolean sendCodeMS(String phone, String code, String name) {
		if (StringUtils.isBlank(name)) {
			name = "优惠券";
		}
		String content = "【" + name + "】验证码：" + code + "，请勿转发，本验证码5分钟内有效。如非本人操作，请忽略此短信。";
		String result = sendMS(phone, content);
		if (result != null && !result.contains("-")) {
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * @author Link
	 * @date 2019-06-24 11:43
	 * @param phone 手机号码
	 * @param code  验证码
	 * @param name  签名
	 * @return
	 */
	public synchronized boolean sendWarningMS(String phone, String title, String msg) {
		if (StringUtils.isBlank(title)) {
			title = "提醒";
		}
		String content = "【" + title + "】" + msg;
		String result = sendMS(phone, content);
		if (result != null && !result.contains("-")) {
			return true;
		}
		return false;
	}

	private String sendMS(String phone, String content) {
		Map<String, String> params = new HashMap<String, String>();
		String md5Pwd = DigestUtils.md5DigestAsHex(this.pwd.getBytes());
		md5Pwd = DigestUtils.md5DigestAsHex((username + md5Pwd).getBytes());
		params.put("username", username);
		params.put("password", md5Pwd);
		params.put("mobile", phone);
		params.put("content", content);

		log.info("username->{}", username);
		log.info("password->{}", md5Pwd);
		log.info("mobile->{}", phone);
		log.info("content->{}", content);

		String result = "";
		try {
			result = WebUtils.doPost(url, params, "UTF-8", 1000 * 10, 1000 * 10);
		} catch (IOException e) {
			e.printStackTrace();
			log.error("发送短信异常:"+e.getMessage());
			return null;
		}
		log.info("response->{}", result);
		return result;
	}
}
